package com.mgetool.ui.animation;

import java.util.Vector;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

import com.mgetool.ui.DrawableInterface;
import com.mgetool.ui.XRect;
import com.mgetool.ui.drawer.BitmapDrawerParam;
import com.mgetool.ui.drawer.ScallingParam;



/**
 * 动画帧 组成动画的必要元素，每帧由0或n个ImageSplitTile组成
 * @author wangbin
 *
 */
public class Frame {
//	private IBitmap ibitmap;
	private Vector<ImageSplitTile> istList;
	private int delay;
//	private IBitmapDrawable bitmap;
	private Vector<XRect> collideRegions;
	private Point offset;
//	public IBitmap getIBitmap() {
//		return ibitmap;
//	}

//	public void setIBitmap(IBitmap bitmap) {
//		this.ibitmap = bitmap;
//	}

	public void addImageSplitTile(DrawableInterface drawable){
		ImageSplitTile imageSplitTile = new ImageSplitTile(drawable);
		addImageSplitTile(imageSplitTile);
	}
			
	
	public void addImageSplitTile(ImageSplitTile imageSplitTile){
		if(istList == null){
			istList = new Vector<ImageSplitTile>();
		}
		istList.addElement(imageSplitTile);
	}
	
	public Frame(DrawableInterface drawable, int delay) {
		addImageSplitTile(drawable);
		setDelay(delay);
	}
	
	public Frame(){
		
	}
	
//	public Frame(IBitmap bitmap, int delay) {
//		addImageSplitTile(bitmap);
//		setDelay(delay);
//	}

//	public Frame(String bitmapUrl, int delay) {
//		addImageSplitTile(bitmapUrl);
//		this.delay = delay;
//	}
	
	public int getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}
	
	public void release(){
		if(istList != null){
			for(int i = 0; i < istList.size(); i++){
				ImageSplitTile imageSplitTile = istList.elementAt(i);
				imageSplitTile.onDestroy();
			}
			istList.removeAllElements();
		}
//		ResourceManager.getInstance().releaseResource(hashCode(), true);
	}
	
	public void draw(GC gc, int x, int y, int rotate, ScallingParam scaling, int alpha){
		int xoff = x;
		int yoff = y;
		if(getOffset() != null){
			if(scaling != null /*&& scaling.getScalling() != null*/){
				xoff += getOffset().x * scaling.getScallingX();
				yoff += getOffset().y * scaling.getScallingY();
			}else{
				xoff += getOffset().x;
				yoff += getOffset().y;
			}
		}
//		if(getBitmap() != null){
//			BitmapDrawerParam param = null;
//			if(alpha != -1 || scaling != null || rotate != 0){
//				param = new BitmapDrawerParam(rotate, scaling, alpha);
//			}
//			bitmap.setParam(param);
//			bitmap.onDraw(canvas, xoff, yoff, paint);
//		}else if(getIBitmap() != null){
//			getIBitmap().draw(canvas, xoff, yoff, paint, rotate, scaling, alpha);
//		}
		
		if(istList != null){
			for(int i = 0; i < istList.size(); i++){
				ImageSplitTile imageSplitTile = istList.elementAt(i);
				BitmapDrawerParam param = null;
				if(alpha != -1 || scaling != null || rotate != 0){
					param = new BitmapDrawerParam(rotate, scaling, alpha);
				}
//				if(imageSplitTile.getBitmap() != null){
//					imageSplitTile.getBitmap().setDrawerParam(param);
//				}else if(imageSplitTile.getIBitmap() != null){
//					imageSplitTile.getIBitmap().setParam(param);
//				}
				imageSplitTile.setDrawerParam(param);
				imageSplitTile.onDraw(gc, xoff, yoff);
			}
		}
		
	}

//	public IBitmapDrawable getBitmap() {
//		return bitmap;
//	}

//	public void setBitmap(Bitmap bitmap) {
//		this.bitmap = new IBitmapDrawable(bitmap);
////		drawer = new BitmapDrawer(bitmap);
//	}
	
	/**
	 * 得到帧的宽度
	 * @return
	 */
	public int getWidth(){
		if(istList != null && istList.size() > 0){
			ImageSplitTile imageSplitTile = istList.elementAt(0);
//			if(imageSplitTile.getBitmap() != null){
//				return imageSplitTile.getBitmap().getWidth();
//			}
//			if(imageSplitTile.getIBitmap() != null){
//				return imageSplitTile.getIBitmap().getWidth();
//			}
			return imageSplitTile.getWidth();
		}
		return 0;
	}
	
	/**
	 * 得到帧的高度
	 * @return
	 */
	public int getHeight(){
		if(istList != null && istList.size() > 0){
			ImageSplitTile imageSplitTile = istList.elementAt(0);
//			if(imageSplitTile.getBitmap() != null){
//				return imageSplitTile.getBitmap().getHeight();
//			}
//			if(imageSplitTile.getIBitmap() != null){
//				return imageSplitTile.getIBitmap().getHeight();
//			}
			return imageSplitTile.getHeight();
		}
		return 0;
	}

	/**
	 * 得到碰撞区域列表
	 * @return
	 */
	public Vector<XRect> getCollideRegions() {
		return collideRegions;
	}
	
	/**
	 * 设置碰撞区域列表
	 * @param collideRegions
	 */
	public void setCollideRegions(Vector<XRect> collideRegions) {
		this.collideRegions = collideRegions;
	}
	
	/**
	 * 添加碰撞区域
	 * 碰撞矩形区域左上角坐标，为该帧左上角坐标的偏移，非绝对坐标点
	 * @param rect
	 */
	public void addCollideRegion(XRect rect){
		if(collideRegions == null){
			collideRegions = new Vector<XRect>();
		}
		collideRegions.addElement(rect);
	}

	public Point getOffset() {
		return offset;
	}

	public void setOffset(Point offset) {
		this.offset = offset;
	}
	
	
	
}